// 云函数入口文件
const cloud = require('wx-server-sdk')
const md5 = require('md5');

cloud.init({
  env:"dev-e0zmh"
})
const db = cloud.database();

// 云函数入口函数
exports.main = async (event) => {
  console.log(event);

  const {qid,answers, userInfo} = event;
  const recordId = md5(qid + userInfo.openId);
  const redordData = {
    qid:qid,
    answers:answers,
    openId:userInfo.openId,
  }

  // console.log(redordData);

  const queryResult = await db.collection('answer')
    .doc(recordId)
    .set({
      data: redordData,
    });

    if(queryResult.errMsg == 'document.set:ok'){
      return {
        errcode:0,
        errmsg:"OK",
        data:queryResult.stats,
      }
    }else{
      return {
        errcode:1,
        errmsg:queryResult.errMsg,
      }
    }
}